﻿Public Class frmFilter_Scale3x

    Private _Bmp As TheiaLib.Imaging.HDRBitmap

    Public Sub New()
        InitializeComponent()
    End Sub

    Public Sub New(ByVal BMP As TheiaLib.Imaging.HDRBitmap)
        InitializeComponent()
        _Bmp = BMP

        If _Bmp.Width > _Bmp.Height Then
            Dim OrgHeight As Integer = pbInputImage.Height
            pbInputImage.Height = pbInputImage.Width / _Bmp.Width * _Bmp.Height
            pbInputImage.Top += (OrgHeight - pbInputImage.Height) / 2
            pnlPreviewPosition.Top = pbInputImage.Top
        Else
            Dim OrgWidth As Integer = pbInputImage.Width
            pbInputImage.Width = pbInputImage.Height / _Bmp.Height * _Bmp.Width
            pbInputImage.Left += (OrgWidth - pbInputImage.Width) / 2
            pnlPreviewPosition.Left = pbInputImage.Left
        End If

        pnlPreviewPosition.Width = Math.Min(pbPreview.Width / _Bmp.Width * pbInputImage.Width, pbInputImage.Width)
        pnlPreviewPosition.Height = Math.Min(pbPreview.Height / _Bmp.Height * pbInputImage.Height, pbInputImage.Height)
        pnlPreviewPosition.BackColor = System.Drawing.Color.Transparent

        pbInputImage.Image = _Bmp.AsBitmap
        pbInputImage.SizeMode = PictureBoxSizeMode.Zoom

        pnlPreviewPosition.Visible = True

    End Sub

    Private Sub UpdatePreviewPosPnl()
        'pnlPreviewPosition.Visible = False
        'pnlPreviewPosition.CreateGraphics.CopyFromScreen(Me.PointToScreen(pnlPreviewPosition.Location), New Drawing.Point(0, 0), pnlPreviewPosition.Size)

        'pnlPreviewPosition.Visible = True
    End Sub

    Dim pnlPreviewPos_MoveStartPoint As System.Drawing.Point
    Dim pnlPreviewPosMoving As Boolean = False

    Private Sub pnlPreviewPosition_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pnlPreviewPosition.MouseDown
        pnlPreviewPos_MoveStartPoint = Me.PointToScreen(e.Location)
        pnlPreviewPosMoving = True
    End Sub

    Private Sub pnlPreviewPosition_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pnlPreviewPosition.MouseMove
        If pnlPreviewPosMoving Then
            Dim pnlPreviewPos_MoveEndPoint As System.Drawing.Point = pnlPreviewPosition.PointToScreen(e.Location)
            pnlPreviewPos_MoveEndPoint.Offset(-pnlPreviewPos_MoveStartPoint.X, -pnlPreviewPos_MoveStartPoint.Y)
            pnlPreviewPos_MoveEndPoint.X = Math.Max(Math.Min(pnlPreviewPos_MoveEndPoint.X, pbInputImage.Width - pnlPreviewPosition.Width + pbInputImage.Left), pbInputImage.Left)
            pnlPreviewPos_MoveEndPoint.Y = Math.Max(Math.Min(pnlPreviewPos_MoveEndPoint.Y, pbInputImage.Height - pnlPreviewPosition.Height + pbInputImage.Top), pbInputImage.Top)
            pnlPreviewPosition.Location = pnlPreviewPos_MoveEndPoint

            UpdatePreviewPosPnl()

        End If

    End Sub

    Private Sub pnlPreviewPosition_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pnlPreviewPosition.MouseUp
        If pnlPreviewPosMoving Then
            pnlPreviewPosMoving = False

            Dim img As Drawing.Image = pbInputImage.Image
            Dim OfsX As Integer = pnlPreviewPosition.Left - pbInputImage.Left
            Dim OfsY As Integer = pnlPreviewPosition.Top - pbInputImage.Top
            Dim factor As Double
            If pbInputImage.Image.Width > pbInputImage.Image.Height Then
                factor = pbInputImage.Image.Width / pbZoomedInputImage.Width
            Else
                factor = pbInputImage.Image.Height / pbZoomedInputImage.Height
            End If
            Dim ToRect As New Drawing.Rectangle(0, 0, pbZoomedInputImage.Width, pbZoomedInputImage.Height)
            Dim FromRect As New Drawing.Rectangle(factor * OfsX, _
                                                  factor * OfsY, _
                                                  pbZoomedInputImage.Width, _
                                                  pbZoomedInputImage.Height)

            GroupBox1.Text = pbInputImage.Width & ":" & pbInputImage.Height & "  /  " & OfsX & " : " & OfsY

            pbZoomedInputImage.CreateGraphics.DrawImage(img, ToRect, FromRect, GraphicsUnit.Pixel)


            Dim f As New TheiaFilter.LDR.Improve.Scale3X(pbInputImage.Image)
            f.Apply()
            pbPreview.Image = f.OutputImage

        End If

    End Sub

    Private Sub frmFilter_Scale3x_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Dim pbPreview_MoveStartPoint As System.Drawing.Point
    Dim pbPreviewMoving As Boolean = False

    Private Sub pbPreview_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pbPreview.MouseDown
        pbPreview_MoveStartPoint = Me.PointToScreen(e.Location)
        pbPreviewMoving = True
    End Sub

    Private Sub pbPreview_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pbPreview.MouseMove
        If pbPreviewMoving Then
            Dim pbPreview_MoveEndPoint As System.Drawing.Point = pbPreview.PointToScreen(e.Location)
            pbPreview_MoveEndPoint.Offset(-pnlPreviewPos_MoveStartPoint.X, -pnlPreviewPos_MoveStartPoint.Y)
            pbPreview_MoveEndPoint.X = Math.Max(Math.Min(pbPreview_MoveEndPoint.X, pbInputImage.Width - pnlPreviewPosition.Width + pbInputImage.Left), pbInputImage.Left)
            pbPreview_MoveEndPoint.Y = Math.Max(Math.Min(pbPreview_MoveEndPoint.Y, pbInputImage.Height - pnlPreviewPosition.Height + pbInputImage.Top), pbInputImage.Top)
            pnlPreviewPosition.Location = pbPreview_MoveEndPoint
        End If
    End Sub

    Private Sub pbPreview_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pbPreview.MouseUp

    End Sub

End Class